﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _1945
{
    public enum BackgroundStyle
    {
        Stretch,
        Tiled,
        Center,
        None
    }

    public class ScrollingBackground
    {
        Texture2D image;
        public BackgroundStyle Style = BackgroundStyle.Stretch;
        public Vector2 Offset = Vector2.Zero;
        public Vector2 Speed = Vector2.Zero;

        public ScrollingBackground(Texture2D backgroundImage, BackgroundStyle backgroundStyle, Vector2 speed)
        {
            //Sets the values
            image = backgroundImage;
            Style = backgroundStyle;
            Speed = speed;
        }

        public void Draw(SpriteBatch spriteBatch, GameWindow gameWindow)
        {
            //Adjusts the background offset based on the speed
            Offset += Speed;
            if (Style == BackgroundStyle.Tiled)
            {
                //Resets the offset if it isn't needed. If we don't do this the game might slow down having to draw so many extra backgrounds, or the background might scroll offscreen.
                if (Offset.X >= 0)
                {
                    Offset.X -= image.Width;
                }
                if (Offset.Y >= 0)
                {
                    Offset.Y -= image.Height;
                }
                if (Offset.X <= -image.Width)
                {
                    Offset.X += image.Width;
                }
                if (Offset.Y <= -image.Height)
                {
                    Offset.Y += image.Height;
                }
            }
            //Draw the background
            spriteBatch.Begin();
            if (Style == BackgroundStyle.Stretch)
            {
                //Draws the background stretched to fit the screen
                spriteBatch.Draw(image, new Rectangle(0, 0, gameWindow.ClientBounds.Width, gameWindow.ClientBounds.Height), Color.White);
            }
            else if (Style == BackgroundStyle.Tiled)
            {
                //Tiles the background based on the offset
                for (int currentx = (int)Offset.X; currentx < gameWindow.ClientBounds.Width; currentx += image.Width)
                {
                    for (int currenty = (int)Offset.Y; currenty < gameWindow.ClientBounds.Height; currenty += image.Height)
                    {
                        spriteBatch.Draw(image, new Rectangle(currentx, currenty, image.Width, image.Height), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 1);
                    }
                }
            }
            else if (Style == BackgroundStyle.Center)
            {
                Offset = new Vector2(gameWindow.ClientBounds.Width / 2 - image.Width / 2, gameWindow.ClientBounds.Height / 2 - image.Height / 2);
                spriteBatch.Draw(image, new Rectangle((int)Offset.X, (int)Offset.Y, image.Width, image.Height), Color.White);
            }
            else
            {
                spriteBatch.Draw(image, new Rectangle((int)Offset.X, (int)Offset.Y, image.Width, image.Height), Color.White);
            }
            spriteBatch.End();
        }

    }
}

